package com.nantai.jiafang.nantaijiafang.activity;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.nantai.jiafang.nantaijiafang.Data;
import com.nantai.jiafang.nantaijiafang.IConstant;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.activity.adapter.MiaoliaoEveryDayAdapter;
import com.nantai.jiafang.nantaijiafang.base.BaseActivity;
import com.nantai.jiafang.nantaijiafang.databinding.ActivityMianliaoEverydayBinding;

/**
 * Created by admin on 2017/11/24.
 */

public class MianLiaoEveryDay_Activity extends BaseActivity<ActivityMianliaoEverydayBinding> {
    ActivityMianliaoEverydayBinding mbinding;
    MiaoliaoEveryDayAdapter miaoliaoEveryDayAdapter;

    @Override
    public int layoutid() {
        return R.layout.activity_mianliao_everyday ;
    }
    @Override
    public void init(ActivityMianliaoEverydayBinding binding) {
        mbinding=binding;
        int pos=getIntent().getIntExtra("select",0);
        String date=getIntent().getStringExtra(IConstant.INTENT);
        mbinding.tvEveryDate.setText(date.substring(0,4)+"-"+date.substring(4,6)+"-"+date.substring(6,8));
        GridLayoutManager gridLayoutManager=new GridLayoutManager(this,1);
        miaoliaoEveryDayAdapter=new MiaoliaoEveryDayAdapter(this,Data.everyday.get(pos));
        float suma=0.0f,sumb=0.0f;
        for (int i = 0; i <Data.everyday.get(pos).size() ; i++) {
            if (!Data.everyday.get(pos).get(i).getPickLength().isEmpty()) {
                if (Data.everyday.get(pos).get(i).getLevels().equals("A")) {
                    suma += Float.valueOf(Data.everyday.get(pos).get(i).getPickLength());
                } else {
                    sumb += Float.valueOf(Data.everyday.get(pos).get(i).getPickLength());
                }
            }else {
                Log.e("isEmpty", "init: "+i );
            }
        }

        mbinding.tvA.setText(suma+"");
        mbinding.tvB.setText(sumb+"");
        mbinding.recyclerEveryday.setLayoutManager(gridLayoutManager);
        mbinding.recyclerEveryday.setAdapter(miaoliaoEveryDayAdapter);
        mbinding.lineSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] levels=new String[]{"全部","A","B"};
                new AlertDialog.Builder(MianLiaoEveryDay_Activity.this) // build AlertDialog
                        .setTitle("选择等级") // title
                        .setItems(levels, new  DialogInterface.OnClickListener() { //content
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                miaoliaoEveryDayAdapter.levels_select(which);
                                mbinding.tvLevel.setText(levels[which]);
                            }
                        }) .show();

            }
        });
        mbinding.spinnerSelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        miaoliaoEveryDayAdapter.showall();
                        break;
                        default:
                            miaoliaoEveryDayAdapter.showselect(mbinding.spinnerSelect.getSelectedItem().toString());
                            break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        mbinding.tvEveryBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
